package com.frxuexi.repair

import com.blankj.utilcode.util.SPUtils
import com.frxuexi.base.http.ApiService
import com.frxuexi.base.http.Const
import com.frxuexi.base.mvi.BaseViewModel
import com.frxuexi.base.mvi.RequestType
import dagger.hilt.android.lifecycle.HiltViewModel
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File
import javax.inject.Inject

@HiltViewModel
class RepairViewModel @Inject constructor(val apiService: ApiService):BaseViewModel<RepairIntent>() {
    override fun handleIntent(it: RepairIntent) {
        when(it){
            is RepairIntent.getRepairs->httpRequest(apiService.getRepairs())
            is RepairIntent.getRepairByNum->httpRequest(apiService.getRepairByNum(it.num),RequestType.ITEM)
            is RepairIntent.getRepairByName->httpRequest(apiService.getRepairByName(it.name),RequestType.HOME)
            is RepairIntent.upload->{
                httpRequest(apiService.upload(toMultipartBody(it.name,it.path).parts,"repair",SPUtils.getInstance().getInt(Const.UID).toString()))
            }
            is RepairIntent.addRepair->httpRequest(apiService.addRepair(toBody(it.item)),RequestType.ADD)
            is RepairIntent.getRepairById->httpRequest(apiService.getRepairById(it.id))
            is RepairIntent.deleteRepair->httpRequest(apiService.deleteRepair(it.id),RequestType.DELETE)
            is RepairIntent.updateRepair->httpRequest(apiService.updateRepair(it.id,toBody(it.item)),RequestType.UPDATE)
        }
    }
}